package com.chy.线程.顺序打印0to9;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadTest {
    public static void main(String[] args) {
        Lock lock = new Lock();
        for (int i = 0; i < 10; i++) {
            MyRunnable myRunnable = new MyRunnable(lock,i);
            new Thread(myRunnable).start();
        }
    }

    static class Lock2{
        public static int start = 0;
        public static int end = 10;

    }
}

class ThreadTest1{
    private int j;
    public static void main(String args[]) throws InterruptedException {
        ThreadTest1 tt=new ThreadTest1();
        Inc inc=tt.new Inc();
        Dec dec=tt.new Dec();
        for(int i=0;i<2;i++){
            Thread t=new Thread(inc);
            t.start();
            t.join();
            t=new Thread(dec);
            t.start();
//            Thread t2=new Thread(dec);
//            t2.start();
            t.join();
        }
    }
    private synchronized void inc(){
        j++;
        System.out.println(Thread.currentThread().getName()+"-inc:"+j);
    }
    private synchronized void dec(){
        j--;
        System.out.println(Thread.currentThread().getName()+"-dec:"+j);
    }
    class Inc implements Runnable{
        public void run(){
            for(int i=0;i<100;i++){
                inc();
            }
        }
    }
    class Dec implements Runnable{
        public void run(){
            for(int i=0;i<100;i++){
                dec();
            }
        }
    }
}

class CallableThreadTest implements Callable<Integer>
{

    public static void main(String[] args)
    {
        CallableThreadTest ctt = new CallableThreadTest();
        FutureTask<Integer> ft = new FutureTask<>(ctt);
        new Thread(ft,"有返回值的线程").start();

        try
        {
            System.out.println("子线程的返回值："+ft.get());
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        } catch (ExecutionException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public Integer call() throws Exception
    {
        return 5555;
    }

}

class Producer implements Runnable{
    BaoZi baoZi;
    Producer(BaoZi baoZi){
        System.out.println(baoZi);
        this.baoZi = baoZi;
    }
    @Override
    public void run() {
        while (true){//不停生产包子
            synchronized (baoZi){
                if (baoZi.num >= 10){
                    System.out.println("包子很多啦，休息一会~");
//                    baoZi.notifyAll();
                    try {
                        baoZi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    baoZi.num++;
                    System.out.println(Thread.currentThread().getName() + "包子铺造包子，当前剩余包子数量：" + baoZi.num);
                    baoZi.notifyAll();//有包子了，可以来吃了
                }
            }
        }
    }
}
class Consumer implements Runnable{
    BaoZi baoZi;
    Consumer(BaoZi baoZi){
        System.out.println(baoZi);
        this.baoZi = baoZi;
    }
    @Override
    public void run() {
        while (true){//不停吃包子
            synchronized (baoZi){
                if (baoZi.num <= 0){
                    System.out.println("没包子了~");
//                    baoZi.notifyAll();
                    try {
                        baoZi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    baoZi.num--;
                    System.out.println(Thread.currentThread().getName() + "吃包子，当前剩余包子数量：" + baoZi.num);
                    baoZi.notifyAll();
                }
            }
        }
    }
}
class BaoZi{
    int num;
}
class Test{
    public static void main(String[] args) {
        BaoZi baoZi = new BaoZi();

        new Thread(new Producer(baoZi)).start();
        new Thread(new Producer(baoZi)).start();
        new Thread(new Consumer(baoZi)).start();
        new Thread(new Consumer(baoZi)).start();

    }

}

class ThreadTest2{
    public static void main(String[] args) throws Exception{
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true){

                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }
}

class Singleton1{
    private static final Singleton1 singleton1 = new Singleton1();
    private Singleton1(){}
    public Singleton1 getInstance(){
        return singleton1;
    }
}

class Singleton2{
    private static Singleton2 singleton2;
    private Singleton2(){}
    public Singleton2 getInstance(){
        if (singleton2 == null){
            singleton2 = new Singleton2();
        }
        return singleton2;
    }
}

class Singleton3{
    private volatile static Singleton3 singleton3;
    private Singleton3(){}
    public Singleton3 getInstance(){
        if (singleton3 == null){
            synchronized (Singleton3.class){
                if (singleton3 == null){
                    singleton3 = new Singleton3();
                }
            }
        }
        return singleton3;
    }
}

class ThreadTest4{
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(new MyRunnable2()).start();
        }
    }
}

class MyRunnable2 implements Runnable{
    int a = 1;
    @Override
    public void run() {
        System.out.println(a);
    }
}